<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="height: 99%;"><head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Project - CANopenNode</title>
  <style type="text/css">
    body{
       background-color: rgb(165, 186, 221);
       margin: 0px;
       height: 100%;
    }
    table{
      width: 100%;
    }
    td {
      border: 1px solid black;
      padding: 1px 0.6em 1px 0.3em;
      width: 50%;
    }
    table div{
      position: relative;
      height: 2em;
      margin-top: 0.3em;
    }
    table pre{
      margin: 0px;
    }
    textarea{
      width: 100%;
      height: 10em;
    }
    .right{
      position: absolute;
      right: 0px;
    }
  </style>
  <script type="text/javascript">
    var editorWindowOK = false;

    function loadProject(name){
      var loc = getURLPath(name);
      var req = new XMLHttpRequest();
      req.open('GET', loc, false); 
      req.send(null);
      document.getElementById('textXML').value = req.responseText;
    }

    function updateFields(){
      var xmlText = document.getElementById("textXML");
      var el = xmlText.value.substring(xmlText.value.indexOf("<file"));
      var el = el.substring(0, el.indexOf("/>")+2);
		var filename = ""
		if(el.length){
			var el = new XML(el);
	      fileName = el.@fileName.toString();
			document.title = fileName + " - CANopenNode";
			var span = document.getElementById("title");
			span.replaceChild(document.createTextNode(fileName), span.firstChild);

	      var date = "V " + el.@fileVersion.toString() + "; Updated: " + el.@fileCreationDate.toString() + ", " +
							el.@fileCreationTime.toString() + ", by " + el.@fileCreator.toString();
	      span = document.getElementById("date");
	      span.replaceChild(document.createTextNode(date), span.firstChild);
		}

      //for use with firefox addon "Save Text Area 0.4.5"
      //https://addons.mozilla.org/en-US/firefox/addon/1046
      updateFileName("textXML", "_project.xml");
      updateFileName("textHTM", fileName+".html");
      updateFileName("textODH", "CO_OD.h");
      updateFileName("textODC", "CO_OD.c");
      updateFileName("textEDS", fileName+".eds");
      updateFileName("textXDD", fileName+".xdd");

      document.getElementById("iframe").src = "";
    }

    function openEditor(newWindow){
      var path = document.getElementById("locEditor").value;
      var loc = getURLPath(path + "/device.xul");

      if(newWindow){
        editorWindow = window.open(loc, '_blank', 'left=0,top=150,width=1024,height=768,resizable=yes,scrollbars=yes,location=no,status=no');
        editorWindow.focus();
      }
      else{
        var iframe = document.getElementById("iframe");
        iframe.src = loc;
        editorWindow = iframe.contentWindow;
      }

      document.getElementById("textHTM").value = "-";
      document.getElementById("textODH").value = "-";
      document.getElementById("textODC").value = "-";
      document.getElementById("textEDS").value = "-";
      document.getElementById("textXDD").value = "-";

      //verify if editor window is OK
      editorWindowOK = false;
      setTimeout(verifyEditorWindow, 10000);
    }

    function verifyEditorWindow(){
      if(editorWindowOK == false)
        alert("Error with Editor!\n(Wrong location)");
    }

    function openAbout(){
      var path = document.getElementById("locEditor").value;
      var loc = getURLPath(path + "/about.html");
      var w = window.open(loc, 'About', 'left=0,top=150,height=600,width=800,resizable=yes,scrollbars=yes,location=yes,status=yes');
      w.focus();
    }

    function receiveMessage(event){
      //function receives a message from editor window, which is editing a project file. This event is triggered
      //after editor sends some data. For more information see http://developer.mozilla.org/en/DOM/window.postMessage

      //if (event.origin !== "http://example.com:8080") return; //for safety

      var head = event.data.substring(0, 3);  //first three characters of the message
      var body = event.data.substring(3);    //other text of the message

      switch(head){
        case "RDY":  //editor was loaded
          editorWindow.postMessage(document.getElementById('textXML').value, '*');
          document.getElementById("fileBoxes").style.display = "none";
          document.getElementById("iframe").style.height = "100%";
          editorWindowOK = true;
          break;
        case "CLS":  //editor was closed
          document.getElementById("iframe").style.height = "0px";
          document.getElementById("fileBoxes").style.display = "block";
          break;
        case "XML":  //xml contents was sent
          var tb = document.getElementById("textXML");
          tb.value = body;
          //tb.setAttribute("value", body); //faster code
          updateFields();
          break;
        case "HTM":  //html contents was sent
          var tb = document.getElementById("textHTM");
          tb.value = body;
          //tb.setAttribute("value", body); //faster code
          break;
        case "ODH":  //CO_OD.h contents was sent
          var tb = document.getElementById("textODH");
          tb.value = body;
          //tb.setAttribute("value", body); //faster code
          break;
        case "ODC":  //CO_OD.c contents was sent
          var tb = document.getElementById("textODC");
          tb.value = body;
          //tb.setAttribute("value", body); //faster code
          break;
        case "EDS":  //EDS contents was sent
          var tb = document.getElementById("textEDS");
          tb.value = body;
          //tb.setAttribute("value", body); //faster code
          break;
        case "XDD":  //XDD contents was sent
          var tb = document.getElementById("textXDD");
          tb.value = body;
          //tb.setAttribute("value", body); //faster code
          break;
        default: alert("Message command was not recognized: " + head);
      }
    }
    window.addEventListener("message", receiveMessage, false);

    function viewFile(value, pre){
      var w = window.open("", '_blank', 'left=0,top=150,width=1024,height=768,resizable=yes,scrollbars=yes,location=yes,status=yes,menubar=yes');
      w.document.open();
      if(pre){
        value = value.replace(/&/g, "&amp;");
        value = value.replace(/</g, "&lt;");
        value = value.replace(/>/g, "&gt;");
        w.document.write("<pre>"+value+"</pre>");
      }
      else{
        w.document.write(value);
        //disable anchors
        var anchors = w.document.getElementsByTagName("a");
        for(var i = 0; i < anchors.length; i++){
          anchors[i].removeAttribute("href");
        }
      }
      w.document.close();
    }

    function getURLPath(path){
      //Function returns absolute path based on input path. If input path is relative, it is relative to path of this html document.
      //Input path can be one of the following (either '/' or '\' can be used):
        //- http://www.example.com/example/example.html
        //- file:///C:/example/example.html
        //- C:/example/example.html
        //- example/example.html
        //- /example/example.html
        //- ../example/example.html
      var absloutePath;
      path = path.replace(/\\/g,"/");
      var i = path.indexOf(":");
      if(i >= 0){
        absloutePath = path;
        if(i == 1) absloutePath = "file:///" + absloutePath;
      }
      else{
        absloutePath = location.href;
        absloutePath = absloutePath.substring(0, absloutePath.lastIndexOf("/"));
        while(path.substring(0, 3) == "../"){
          path = path.substring(3);
          absloutePath = absloutePath.substring(0, absloutePath.lastIndexOf("/"));
        }
        var hash = "/";
        if(path.charAt(0) == "/") hash = "";
        absloutePath = absloutePath + hash + path;
      }
      return absloutePath;
    }

    function getLocalPath(path){
      //Function returns absolute path based on input path. If input path is relative, it is relative to path of this html document.
      //Input path can be one of the following (either '/' or '\' can be used):
        //- C:\example\example.html
        //- example\example.html
        //- \example\example.html
        //- ..\example\example.html
      path = path.replace(/\//g,"\\");
      if(path.indexOf(":") >= 0) return path;

      var absloutePath = location.href;
      if(absloutePath.substring(0, 8) != "file:///") return "";
      absloutePath = absloutePath.substring(8, absloutePath.lastIndexOf("/"));
      absloutePath = absloutePath.replace(/\//g,"\\");
      while(path.substring(0, 3) == "..\\"){
        path = path.substring(3);
        absloutePath = absloutePath.substring(0, absloutePath.lastIndexOf("\\"));
      }
      var hash = "\\";
      if(path.charAt(0) == "\\") hash = "";
      absloutePath = absloutePath + hash + path;
      return absloutePath;
    }

    function updateFileName(field, name){
      document.getElementById(field).setAttribute("fileName", name);
    }
  </script>
</head><body onload="loadProject('_project.xml'); updateFields();">
<div id="fileBoxes" style="margin: 8px; display: block;">
<div style="padding: 0.5em;">
  <span id="title" style="font-size: 1.5em; font-weight: bold;">---</span>
  <span id="date" style="margin-left: 2em;">---</span>
  <input style="margin-left: 2em; font-size: 1.3em;" value="Open Editor" onclick="openEditor(false);" type="button">
  at location:
  <input id="locEditor" value="../../../Object_Dictionary_Editor" onblur="this.setAttribute('value', this.value);" type="text">
  <input class="right" style="right: 1em;" value="Help" onclick="openAbout();" type="button">
</div>
<table>
  <tbody><tr>
    <td>
      <div>
        <b>CANopenNode project file:</b>
        <input class="right" value="View source" onclick="viewFile(document.getElementById('textXML').value, true);" type="button">
      </div>
      <pre><textarea id="textXML" fileName="_project.xml" wrap="off" onchange="updateFields();">-</textarea></pre>
    </td>
    <td>
      <div>
        <b>Documentation HTML file:</b>
        <input value="Preview" onclick="viewFile(document.getElementById('textHTM').value, false);" type="button">
        <input class="right" value="View source" onclick="viewFile(document.getElementById('textHTM').value, true);" type="button">
      </div>
      <textarea id="textHTM" fileName="documentation.html" wrap="off">-</textarea>
    </td>
  </tr><tr>
    <td>
      <div>
        <b>Source code CO_OD.h file:</b>
        <input class="right" value="View source" onclick="viewFile(document.getElementById('textODH').value, true);" type="button">
      </div>
      <textarea id="textODH" fileName="CO_OD.h" wrap="off">-</textarea>
    </td>
    <td>
      <div>
        <b>Source code CO_OD.c file:</b>
        <input class="right" value="View source" onclick="viewFile(document.getElementById('textODC').value, true);" type="button">
      </div>
      <textarea id="textODC" fileName="CO_OD.c" wrap="off">-</textarea>
    </td>
  </tr>
  <tr>
    <td>
      <div>
        <b>EDS specification file:</b>
        <input class="right" value="View source" onclick="viewFile(document.getElementById('textEDS').value, true);" type="button">
      </div>
      <textarea id="textEDS" fileName="Electronic data sheet.eds" wrap="off">-</textarea>
    </td>
    <td>
      <div>
        <b>Errors and warnings:</b>
        <input class="right" value="View source" onclick="viewFile(document.getElementById('textXDD').value, true);" type="button">
      </div>
      <textarea id="textXDD" fileName="Electronic data sheet.xdd" wrap="off">-</textarea>
    </td>
  </tr>
</tbody></table>
<em style="text-align: center; display: block;"><a href="http://canopennode.sourceforge.net/">CANopenNode</a> Editor - project file.</em>
</div>
<iframe style="border: 0px none ; width: 100%; height: 0px;" src="" id="iframe"></iframe>
</body></html>